#if !UNITY_WINRT
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Reflection;
using System.Collections.Generic;
using Mono.Security;
using Mono.Security.Cryptography;

namespace UnityEngine
{
	public sealed partial class Security
	{
	#if UNITY_EDITOR || UNITY_WEBPLAYER
		static List<Assembly> _verifiedAssemblies = new List<Assembly>();
	#endif

		static MethodInfo GetUnityCrossDomainHelperMethod(string methodname)
		{
			//todo: enter strong name with public key here
			Type type = Types.GetType ("UnityEngine.UnityCrossDomainHelper", "CrossDomainPolicyParser, Version=1.0.0.0, Culture=neutral");
			if (type == null)
				throw new SecurityException("Cant find type UnityCrossDomainHelper");
			var result = type.GetMethod (methodname);
			if (result == null)
				throw new SecurityException("Cant find "+methodname);
			return result;
		}
		
		internal static string TokenToHex (byte[] token)
		{
			if (null == token || 8 > token.Length)
				return string.Empty;
			
			return string.Format ("{0:x2}{1:x2}{2:x2}{3:x2}{4:x2}{5:x2}{6:x2}{7:x2}",
				token[0],
				token[1],
				token[2],
				token[3],
				token[4],
				token[5],
				token[6],
				token[7]
			);
		}
		
		#if UNITY_EDITOR
		internal static void ClearVerifiedAssemblies ()
		{
			_verifiedAssemblies.Clear ();
		}
		#endif

		[SecuritySafeCritical]
		public static Assembly LoadAndVerifyAssembly (byte[] assemblyData)
		{
		#if UNITY_EDITOR || UNITY_WEBPLAYER
			var assembly = Assembly.Load (assemblyData);
			byte[] publicKey = assembly.GetName ().GetPublicKey ();
			if (null == publicKey || 0 == publicKey.Length)
				return null;

			var rsa = new RSACryptoServiceProvider ();
			rsa.ImportCspBlob (publicKey);
			var strongName = new StrongName (rsa);
			using (var stream = new MemoryStream (assemblyData)) 
			{
				if (strongName.Verify (stream))
				{
					_verifiedAssemblies.Add(assembly);
					return assembly;
				} else
				{
					return null;
				}
			}
		#else
			return null;
		#endif
		}

#if UNITY_EDITOR
		static readonly string kSignatureExtension = ".signature";

		internal static bool VerifySignature (string file, byte[] publicKey)
		{
			try {
				string signature = file + kSignatureExtension;
				if (!File.Exists (signature))
					return false;

				using (var provider = new RSACryptoServiceProvider ())
				{
					provider.ImportCspBlob (publicKey);
					using (var sha1 = new SHA1CryptoServiceProvider ())
						return provider.VerifyData (File.ReadAllBytes (file), sha1, File.ReadAllBytes (signature));
				}
			} catch (Exception e) {
				Debug.LogException (e);
			}
			return false;
		}
#endif
	}

	public static class Types
	{
		public static Type GetType(string typeName, string assemblyName)
		{
			try
			{
				return Assembly.Load(assemblyName).GetType(typeName);
			}
			catch (Exception)
			{
				return null;
			}
		}
	}
}
#endif